<template>
  <div class="home-content">
    <!-- 导航菜单 -->
    <div class="home-content-left">
      <div class="left-menu">
        <el-row class="tac">
          <el-col :span="24">
            <el-menu
              :default-active="activeNow"
              class="el-menu-vertical-demo"
              background-color="#7B1FA2"
              text-color="#fff"
              active-text-color="#FF5722"
              router
              @select="selectMenu"
              unique-opened
              :style="(width = 200)"
            >
              <el-menu-item index="/home/personal">
                <div class="img-h">
                  <img :src="imgurl" />
                  <h4>商家中心</h4>
                </div>
              </el-menu-item>
              <div v-for="item in treelist" :key="item.text">
                <el-submenu v-if="item.children" :index="item.index">
                  <template slot="title">
                    <i :class="item.i"></i>
                    <span>{{ item.text }}</span>
                  </template>
                  <el-menu-item-group
                    v-for="val in item.children"
                    :key="val.text"
                  >
                    <el-menu-item :index="val.index">{{
                      val.text
                    }}</el-menu-item>
                  </el-menu-item-group>
                </el-submenu>
                <el-menu-item v-else :index="item.index">
                  <i :class="item.i"></i>
                  <span slot="title">{{ item.text }}</span>
                </el-menu-item>
              </div>
            </el-menu>
          </el-col>
        </el-row>
      </div>
    </div>
    <!-- 右边内容 -->
    <div class="home-content-right">
      <div class="home-content-header">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item v-for="item in breadlist" :key="item">{{
            item
          }}</el-breadcrumb-item>
        </el-breadcrumb>
        <div class="head">
          <span>你好！{{ username }}</span>
          <img @click="userinfo" :src="imgurl" alt />
        </div>
      </div>
      <div class="home-content-right-right">
        <router-view class="router"></router-view>
      </div>
    </div>
  </div>
</template>

<script>
import { accountinfo, checktoken } from "@/apis/users.js";
import { bus } from "@/utils/utils";
export default {
  data() {
    return {
      username: "",
      imgurl: "",
      breadlist: [],
      treelist: [
        {
          index: "/home/home-content",
          i: "iconfont icon-home",
          text: "后台首页",
          bread: ["首页"],
        },
        {
          index: "/home/order-manager",
          i: "iconfont icon-order3",
          text: "订单管理",
          bread: ["订单管理"],
        },
        {
          index: "3",
          i: "iconfont icon-shopp",
          text: "商品管理",
          bread: ["商品管理"],
          children: [
            {
              index: "/home/goods-list",
              text: "商品列表",
              bread: ["商品管理", "商品列表"],
            },
            {
              index: "/home/goods-add",
              text: "商品添加",
              bread: ["商品管理", "商品添加"],
            },
            {
              index: "/home/goods-class",
              text: "商品分类",
              bread: ["商品管理", "商品分类"],
            },
          ],
        },
        {
          index: "/home/shop-manager",
          i: "iconfont icon-shop",
          text: "店铺管理",
          bread: ["店铺管理"],
        },
        {
          index: "5",
          i: "iconfont icon-user",
          text: "账号管理",
          bread: ["账号管理"],
          children: [
            {
              index: "/home/accont-list",
              text: "账号列表",
              bread: ["账号管理", "账号列表"],
            },
            {
              index: "/home/accont-add",
              text: "添加账号",
              bread: ["账号管理", "添加账号"],
            },
            {
              index: "/home/change-pwd",
              text: "修改密码",
              bread: ["账号管理", "添加账号"],
            },
          ],
        },
        {
          index: "6",
          i: "iconfont icon-Statistics",
          text: "销售统计",
          bread: ["销售统计"],
          children: [
            {
              index: "/home/goods-total",
              text: "商品统计",
              bread: ["销售统计", "商品统计"],
            },
            {
              index: "/home/order-total",
              text: "订单统计",
              bread: ["销售统计", "订单统计"],
            },
          ],
        },
      ],
      activeNow: "/home/home-content",
    };
  },
  methods: {
    userinfo() {
      this.$router.push("/home/personal");
    },
    selectMenu(index) {
      // console.log(index);
      let bread = [];
      for (let obj of this.treelist) {
        //判断是否有chilren，有的话循环儿子
        if (obj.children) {
          for (let child of obj.children) {
            if (child.index == index) {
              bread = child.bread; //拿到对应数组
            }
          }
        } else {
          //没有儿子，直接判断索引
          if (obj.index == index) bread = obj.bread;
        }
      }

      //把找到的数据赋值给面包屑数组
      this.breadlist = bread;
      // console.log(bread);
    },
  },
  created() {
    //判断权限
    if (localStorage.role == "normal") {
      this.treelist.splice(3, 2);
    }
    //验证token
    checktoken({ token: localStorage.token })
      .then((res) => {
        //获取数据
        if (res.data.code === 0) {
          accountinfo({ id: localStorage.id })
            .then((res) => {
              this.username = res.data.accountInfo.account;
              this.imgurl = res.data.accountInfo.imgUrl;
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          this.username = "请先登陆";
        }
      })
      .catch((err) => {
        console.log(err);
      });

    this.activeNow = location.hash.replace("#", "");
    this.selectMenu(this.activeNow); //找到当前选项卡的面包屑数组
    //获取传递的头像
    bus.$on("changehead", (img) => {
      this.imgurl = img;
    });
  },
};
</script>

<style lang="less" scoped>
@import url("../assets/css/CommonColor.less");
html {
  height: 100%;
  width: 100%;
}
.home-content {
  display: flex;
  height: 100%;
  .home-content-left {
    width: 200px;
    background-color: @basecolor1;
    height: 100%;
  }
  .home-content-right {
    flex: 1;
    background-color: rgb(238, 238, 238);
    display: flex;
    flex-direction: column;
    overflow: hidden;

    .home-content-header {
      height: 50px;
      width: 100%;
      background-color: @basecolor;
      display: flex;
      align-items: center;
      line-height: 50px;
      justify-content: space-between;
      padding: 0px 20px;
      .head {
        display: flex;
        align-items: center;
        color: white;
        img {
          margin-left: 8px;
          width: 45px;
          height: 45px;
          border-radius: 50%;
          cursor: pointer;
        }
      }
    }
    .home-content-right-right {
      flex: 1;
      overflow: scroll;
      width: 100%;
    }
  }
}
.router {
  padding: 20px;
  height: 100%;
}
.left-menu {
  margin-right: 0px;
  width: 200px;
  ul {
    border: 0px;
  }
}
i {
  margin-right: 8px;
}
img {
  width: 50px;
  height: 50px;
  border-radius: 50%;
}
h4 {
  color: white;
  text-align: center;
  margin-left: 10px;
}
.img-h {
  width: 100%;
  height: 60px;
  display: flex;
  align-items: center;
}
</style>